<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
class Cmodify extends controller {
	
	public function main() {
		$view   = C\view::inst('extjs');
		$userid = Cadmin::chkaccess($view);
		$dlang  = lang::inst('dd');
		$view->assign('type', $dlang->get('coin_type'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/finance/modify.ext.js');
	}
	
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array();
		$userid = input::post('userid');
		if (!empty($userid)) {
			$where[] = array('userid', $userid, \classlib\orm::OP_EQ);
		}
		$type   = input::post('type');
		if (is_numeric($type)) {
			$where[] = array('reftype', $type, \classlib\orm::OP_EQ);
		}
		$sdate  = input::post('sdate');
		if ($sdate) {
			$where[] = array('stime', strtotime($sdate), \classlib\orm::OP_GT);
		}
		$edate  = input::post('edate');
		if ($edate) {
			$where[] = array('stime', strtotime($edate), \classlib\orm::OP_LT);
		}
		$orm    = C\model::load('pro.log.modifycoin');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$state= $orm->get_item($where, 'sum(money) as money, sum(coin) as coin');
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			$uorm = C\model::load('pro.pl.userinfo');
			foreach ($list as &$item) {
				$user = $uorm->get_one($item['userid']);
				$item['anchor'] = $user['nickname'];
				$item['stime']  = date('Y-m-d H:i:s', $item['stime']);
			}
			$list[] = array('userid'=>'statistics', 'money'=>$state['money'], 'coin'=>$state['coin']);
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}

}